package domain;

import util.Direction;
import util.Node;

import java.util.ArrayList;
import java.util.LinkedList;

public class Snake {
    private LinkedList<Node> body = new LinkedList<>();

    private Direction dir = Direction.RIGHT;

    private boolean isLiving = true;

    public Snake() {
        initSnake();
    }

    public Snake(LinkedList<Node> body, Direction dir) {
        this.body = body;
        this.dir = dir;
    }

    public Snake(LinkedList<Node> body, Direction dir, boolean isLiving) {
        this.body = body;
        this.dir = dir;
        this.isLiving = isLiving;
    }

    private void initSnake() {
        body.add(new Node(22, 14));
        body.add(new Node(23, 14));
        body.add(new Node(24, 14));
        body.add(new Node(25, 14));
        body.add(new Node(26, 14));
        body.add(new Node(27, 14));
        body.add(new Node(28, 14));
    }

    public void move() {

        if (isLiving) {
            Node head = body.getFirst();
            switch (dir) {
                case UP: {
                    body.addFirst(new Node(head.getX(), head.getY() - 1));
                    break;
                }
                case DOWN: {
                    body.addFirst(new Node(head.getX(), head.getY() + 1));
                    break;
                }
                case RIGHT: {
                    body.addFirst(new Node(head.getX() + 1, head.getY()));
                    break;
                }
                case LEFT: {
                    body.addFirst(new Node(head.getX() - 1, head.getY()));
                    break;
                }
            }
            body.removeLast();

            head = body.getFirst();

            //判断蛇是否撞自己身体一直实现不了
//            for (int i = 1; i < body.size(); i++) {
//
//                if ((head.getX() == body.get(i).getX()) && (head.getY() == body.get(i).getY())) {
//                    this.isLiving = false;
//                    break;
//                }
//            }


            if (head.getX() == -1 || head.getX() == 50 || head.getY() == -1 || head.getY() == 30) {
                this.isLiving = false;
            }

        }
    }


    /**
     * 获取
     *
     * @return body
     */
    public LinkedList<Node> getBody() {
        return body;
    }

    /**
     * 设置
     *
     * @param body
     */
    public void setBody(LinkedList<Node> body) {
        this.body = body;
    }

    /**
     * 获取
     *
     * @return dir
     */
    public Direction getDir() {
        return dir;
    }

    /**
     * 设置
     *
     * @param dir
     */
    public void setDir(Direction dir) {
        this.dir = dir;
    }

    public String toString() {
        return "Snake{body = " + body + ", dir = " + dir + "}";
    }

    public void eat(Food food) {
        switch (this.dir) {
            case UP: {
                body.addFirst(new Node(food.getFood().getX(), food.getFood().getY()));
                break;
            }
            case DOWN: {
                body.addFirst(new Node(food.getFood().getX(), food.getFood().getY()));
                break;
            }
            case RIGHT: {
                body.addFirst(new Node(food.getFood().getX(), food.getFood().getY()));
                break;
            }
            case LEFT: {
                body.addFirst(new Node(food.getFood().getX(), food.getFood().getY()));
                break;
            }
        }
    }

    /**
     * 获取
     *
     * @return isLiving
     */
    public boolean isIsLiving() {
        return isLiving;
    }

    /**
     * 设置
     *
     * @param isLiving
     */
    public void setIsLiving(boolean isLiving) {
        this.isLiving = isLiving;
    }
}
